package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.commons.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class UserFreezeService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 判断用户是否被冻结，已被冻结，抛出异常
     * 参数：冻结范围
     */
    public void checkUserStatus(String state,Long userId) throws Exception {
        //1.拼接key从redis中查询数据
        String key = Constants.USER_FREEZE + userId;
        String value = redisTemplate.opsForValue().get(key);
        //2.数据存在且冻结范围一致，抛出异常
        if (!StringUtils.isEmpty(value)){
            Map map = JSON.parseObject(value, Map.class);
            String freezingRange =(String) map.get("freezingRange");
            if (state.equals(freezingRange)){
                throw new Exception("用户已被冻结！");
            }
        }
    }

}
